<?php
/**
 * 报名登记类
 * @author Administrator
 *
 */
class EnrollAction extends Action {
	/**
	 * 招生报名页面展示方法
	 */
	public function enroll(){
		//获取招生信息
		$enroll_info = M('Article')->where('ac_id = 14')->select();
		$this->assign('enroll_info',$enroll_info);//输出招生信息
		$this->display();
	}
	/**
	 * 招生信息详情
	 */
	public function enroll_info_detail(){
		$obj = M('Article')->find($_GET['id']);
		$data['visits'] =$obj['visits']+1;
		M('Article')->where('id = '.$_GET['id'])->save($data);
		$this->assign('obj',$obj);
		$this->display();
	}
	/**
	 * 学生报名填表
	 */
	public function write_form(){
		$re = M('Cost')->where('id=2')->find();
		if($re['cost']==0){
			header("Content-type: text/html; charset=utf-8");
			echo '<script type="text/javascript">'."\n";
			echo 'alert("现在不是报名时间");'."\n";
			echo 'window.location.href=document.referrer;'."\n";
			echo '</script>'."\n";
			exit;
		}
		if($_POST){
			$count = M('Enroll')->count();
			$_POST['user_id']=$_SESSION['user_id'];
			$_POST['create_time'] = time();
			if($_POST['prize']){
				$_POST['prize'] = implode(',',$_POST['prize']);//将图片地址数组变成以逗号分隔的字符串
			}
			if($_POST['achievement']){
				$_POST['achievement'] = implode(',',$_POST['achievement']);//将图片地址数组变成以逗号分隔的字符串
			}
			$_POST['enroll_year'] = date('Y',time());
			if($_POST['year'] == 1){
				$max = M('Enroll')->where('enroll_year ='.date('Y',time()).' and year=1')->max('enroll_num');
				if($max){
					$_POST['enroll_num'] = $max+1;
				}else{
					$_POST['enroll_num'] = 30000;
				}
			}
			if($_POST['year'] == 2){
				$max = M('Enroll')->where('enroll_year ='.date('Y',time()).' and year=2')->max('enroll_num');
				if($max){
					$_POST['enroll_num'] = $max+1;
				}else{
					$_POST['enroll_num'] = 40000;
				}
			}
			$_POST['state'] = 0;
			$_POST['order_sn'] = create_order_sn();
			$re = M('Enroll')->data($_POST)->filter('strip_tags')->add();
			session('enroll_id',$re);
			
			
			header('Location:'.U('Enroll/img_create'));
			
			exit;
		}
		if(!$_SESSION['login']){
			header('Location:'.U('Login/login'));
			exit;
		}
		$list = M('EnrollmentArea')->select();//查找招生片区返回array
		$this->assign('list',$list);//向模板输出招生片区变量
		$this->display();
	}
	/**
	 * 图片上传方法
	 */
	public function upload(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 100*1024*1024*102400 ;// 设置附件上传大小
		$upload->allowExts  = array();// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/'.date('Y-m-d',time()).'/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$arr['error'][]=$upload->getErrorMsg();
			$this->ajaxReturn($arr);
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$this->ajaxReturn($info);
		}
	}
	
	
	
	public function img_create(){
		if($_SESSION['login']){
			/**
			**生成用户存根
			 */
			$obj = M('Enroll')->where('id='.$_SESSION['enroll_id'])->find();
			$strArray=str_split($obj['id_card']);
			$id_card=join(" ",$strArray);
			$str_p = str_split($obj['father_phone']);
			$father_phone = join(" ",$str_p);
			$str_m = str_split($obj['mother_phone']);
			$mother_phone = join(" ",$str_m);
			$date = date('Y',$obj['create_time']);
			$dst_path = C('BM_IMG_PATH').'/2014.jpg';
			$dst_stub_path = C('BM_IMG_PATH').'/stub.jpg';
			$src_path = dirname(dirname(dirname(dirname(__FILE__)))).$obj['pic_url'];
			
			
			//创建图片的实例
			$dst = imagecreatefromstring(file_get_contents($dst_path));
			$dst_stub = imagecreatefromstring(file_get_contents($dst_stub_path));
			$src = imagecreatefromstring(file_get_contents($src_path));
			list($src_w, $src_h) = getimagesize($src_path);
			$src_img = imagecreatetruecolor(360, 480);
			imagecopyresampled($src_img,$src,0,0,0,0,360,480,$src_w,$src_h);
			
			//打上文字
			$font = C('BM_IMG_PATH').'/simsun.ttc';//字体
			$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
			$brown = imagecolorallocate($dst, 230, 120, 22);
			imagefttext($dst, 50, 0, 420, 662, $black, $font, $obj['name']);
			imagefttext($dst, 50, 0, 396, 520, $black, $font, $obj['enroll_num']);
			if($obj['sex'] == 1){
				imagefttext($dst, 50, 0, 424, 838, $black, $font, '√');
			}
			if($obj['sex'] == 0){
				imagefttext($dst, 50, 0, 628, 838, $black, $font, '√');
			}
			imagefttext($dst, 48, 0, 408, 982, $black, $font, $id_card);
			imagefttext($dst, 52, 0, 396, 292, $brown, $font, $date);
			//获取水印图片的宽高
			
			
			//如果水印图片本身带透明色，则使用imagecopy方法
			imagecopymerge($dst, $src_img, 1086, 348, 0, 0, 360, 480,100);
					
			/**
			 * 生成后台存根
			 */
			/**
			 * 设置右边内容
			 */
			//打上文字
			$font = C('BM_IMG_PATH').'/simsun.ttc';//字体
			$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
			$brown = imagecolorallocate($dst, 230, 120, 22);
			imagefttext($dst_stub, 50, 0, 1578, 666, $black, $font, $obj['name']);
			imagefttext($dst_stub, 50, 0, 1548, 512, $black, $font, $obj['enroll_num']);
			if($obj['sex'] == 1){
				imagefttext($dst_stub, 50, 0, 1600, 834, $black, $font, '√');
			}
			if($obj['sex'] == 0){
				imagefttext($dst_stub, 50, 0, 1800, 834, $black, $font, '√');
			}
			imagefttext($dst_stub, 48, 0, 1598, 982, $black, $font, $id_card);
			imagefttext($dst_stub, 52, 0, 1566, 296, $brown, $font, $date);
			//获取水印图片的宽高
				
				
			//如果水印图片本身带透明色，则使用imagecopy方法
			imagecopymerge($dst_stub, $src_img, 2269, 346, 0, 0, 360, 480,100);
			/**
			 * 设置左边内容
			*/
			//打上文字
			$font = C('BM_IMG_PATH').'/simsun.ttc';//字体
			$black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
			imagefttext($dst_stub, 46, 0, 348, 650, $black, $font, $obj['name']);
			imagefttext($dst_stub, 46, 0, 348, 498, $black, $font, $obj['enroll_num']);
			if($obj['sex'] == 1){
				imagefttext($dst_stub, 50, 0, 342, 803, $black, $font, '√');
			}
			if($obj['sex'] == 0){
				imagefttext($dst_stub, 50, 0, 500, 803, $black, $font, '√');
			}
			imagefttext($dst_stub, 48, 0, 1598, 982, $black, $font, $id_card);
			imagefttext($dst_stub, 36, 0, 334, 983, $black, $font, $obj['s_city']);
			imagefttext($dst_stub, 36, 0, 584, 983, $black, $font, $obj['s_county']);
			imagefttext($dst_stub, 48, 0, 372, 1270, $black, $font, $father_phone);
			imagefttext($dst_stub, 48, 0, 372, 1370, $black, $font, $mother_phone);
			imagefttext($dst_stub, 46, 0, 80, 258, $black, $font, $date);
			//获取水印图片的宽高
			
			
			//如果水印图片本身带透明色，则使用imagecopy方法
			imagecopymerge($dst_stub, $src_img, 740, 324, 0, 0, 360, 480,100);
			//保存图片
			list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
			$time = time().rand(0000,9999);
			$time1 = time().rand(00000,99999);
			
			header('Content-Type: image/jpeg');
			imagejpeg($dst,C('BM_IMG_PATH').'/'.$time.'.jpg');
			imagejpeg($dst_stub,C('BM_IMG_PATH').'/'.$time1.'.jpg');
			$data['enroll_pic'] = $time.'.jpg';
			$data['stub_pic'] = $time1.'.jpg';
			M('Enroll')->where('id='.$_SESSION['enroll_id'])->save($data);
			imagedestroy($dst);
			imagedestroy($dst_stub);
			imagedestroy($src);
			header('Location:'.U('Payment/wxpay').'/?enroll_id='.$_SESSION['enroll_id']);
		}
		
	}
	
	public function prompt(){
		$obj = M('Enroll')->find($_GET['enroll_id']);
		$this->assign('obj',$obj);
		$this->display();
	}
	
}

?>